package main

// rand example

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

func main() {
	// 1 ioutil.ReadAll()
	var srd *strings.Reader
	srd = strings.NewReader("line1\nline2\nline3\n")
	var bs []byte
	bs, _ = ioutil.ReadAll(srd)
	fmt.Printf("1 ------ioutil.ReadAll----\n%s", bs)

	// 2 ioutil.ReadFile()
	var bs1 []byte
	bs1, err := ioutil.ReadFile("/etc/hosts")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("\n\n2 ------ioutil.ReadFile----\n%s", bs1)

	// 3 ioutil.WriteFile()
	ioutil.WriteFile("/tmp/hosts", bs1, 0666)
	fmt.Printf("\n\n3 ------------the content of /etc/hosts has ioutil.WriteFile() to /tmp/hosts !! --------------\n")

	// 4 ioutil.ReadDir()
	fmt.Printf("\n\n4 ------------ioutil.ReadDir() ------------------- \n")
	var fileItem []os.FileInfo
	fileItem, err = ioutil.ReadDir("/tmp")
	if err != nil {
		log.Fatal(err)
	}
	for _, fi := range fileItem {
		fmt.Printf("%-90s %-22s %-11d %-s\n", fi.Name(), fi.ModTime().Format("2006-01-02 15:04:05"), fi.Size(), fi.Mode())
	}
}
